--[[
Gemböbble
Copyright (c) 2010-2011 Carl Ådahl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
Cannon = 
{
}

local angularSpeed = 0.7
local maxAngle = 80
local image = love.graphics.newImage("img/cannon.png")

function Cannon.new(x,y)
	local s = Actor.new("cannon")
	local sfx = Cannon.sfx
	local angle = 0
	local wasUp
	local sfx = 
			{
				shoot = sound.effect("snd/cannon_shot.mp3")
			}

	function s.init()
		s.enterState("waiting")
		s.setDrawPriority(-1)
	end
	
	function s.getX()
		return x
	end

	function s.getY()
		return y
	end

	function s.draw()
		love.graphics.draw(image, x, y, math.rad(angle), 1, 1, image:getWidth()/2, image:getHeight()/2)
	end
	
	function s.states.loading()
		local colors = nil
		
		repeat
			s.update()
			colors = map.getColors()
			--print("waiting for colors to become available.")
		until #colors > 0
		
		local color = colors[math.random(#colors)]
		local ball = world.spawn(Ball.new(x, y, color))
		
		s.enterState("aiming", ball)
	end

	function s.states.waiting(ball)
		s.waitWhile(ball, "moving")
		s.enterState("ready")
	end

	function s.states.ready()
		s.wait()
	end

	function s.states.aiming(ball)

		-- Ensure the cannon is positioned right, now that we're in gameplay.
		y = love.graphics.getHeight()

		local function checkTurn()
			local stickAxis = love.joystick.getAxis(0,0)
			local deadZone = 0.2
			local stickValid = math.abs(stickAxis) >= deadZone

			local magnitude = math.min(stickAxis^2, 0.5)
			stickAxis = 2 * math.sign(stickAxis) * magnitude

			if stickValid then
				if stickAxis < 0 then
		    	angle = math.max( -maxAngle, angle + stickAxis * angularSpeed * s.dt )
				else
	    		angle = math.min( maxAngle, angle + stickAxis * angularSpeed * s.dt )
				end					
			end

		    if love.keyboard.isDown("left") then
		    	angle = math.max( -maxAngle, angle - angularSpeed * s.dt )
		    end

		    if love.keyboard.isDown("right") then
		    	angle = math.min( maxAngle, angle + angularSpeed * s.dt )
		    end

		    if love.keyboard.isDown("down") then
		    	angle = 0
		    end
		end

		-- Update loop
		while s.update() do
			-- If player pressed UP, and we're ready, fire the ball.
			local isUp = love.keyboard.isDown("up") or love.joystick.isDown(0,0)
		    if isUp and not wasUp then
		    	sfx.shoot()
				world.spawn(FlareEffect.new(x, y, s))
		    	ball.enterState("moving", angle)
		    	s.enterState("waiting", ball)
		    end

			-- Track last key state (annoying)
		    wasUp = isUp

			checkTurn()
		end
	end

	s.init()
	return s
end



